using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VMDECK
    {
        public DataRow Convert2MDECK(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryMDECK(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["MDMANDANT"] = " ".PadRight(2 ); 
         dwReturn["MDSA"] = " ".PadRight(1 ); 
         dwReturn["MDAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["MDAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["MDAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["MDTYP"] = " ".PadRight(1 ); 
         dwReturn["MDAFOLGE"] = " ".PadRight(3 ); 
         dwReturn["MDMODNR"] = " ".PadRight(3 ); 
         dwReturn["MDEINSTNR"] = " ".PadRight(3 ); 
         dwReturn["MDRPOS"] = " ".PadRight(0 ); 
         dwReturn["MDMAT"] = " ".PadRight(14 ); 
         dwReturn["MDVAR"] = " ".PadRight(3 ); 
         dwReturn["MDDAT"] = " ".PadRight(0 ); 
         dwReturn["MDLO"] = " ".PadRight(2 ); 
         dwReturn["MDLB"] = " ".PadRight(2 ); 
         dwReturn["MDLP"] = " ".PadRight(8 ); 
         dwReturn["MDAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["MDMENGE"] = " ".PadRight(0 ); 
         dwReturn["MDEINGET"] = " ".PadRight(0 ); 
         dwReturn["MDZUGET"] = " ".PadRight(0 ); 
         dwReturn["MDRESERV"] = " ".PadRight(0 ); 
         dwReturn["MDVERBR"] = " ".PadRight(0 ); 
         dwReturn["MDMEH"] = " ".PadRight(3 ); 
         dwReturn["MDMATTYP"] = " ".PadRight(1 ); 
         dwReturn["MDERLKZ"] = " ".PadRight(1 ); 
         dwReturn["MDDISPOKZ"] = " ".PadRight(1 ); 
         dwReturn["MDABRKZ"] = " ".PadRight(1 ); 
         dwReturn["MDFILLER"] = " ".PadRight(33 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapMDECK(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveMDECK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryMDECK(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapMDECK(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertMDECK(dwLocal, ITXDBVERSION);
            }
            UpdateMDECK(dwLocal);
        }
        public void InsertMDECK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO MDECK(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryMDECK(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM MDECK WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM MDECK  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "MDECK";
            Return = Result;
            return Return;
        }
        public void DeleteMDECK(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM MDECK   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateMDECK(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE MDECK SET "
                    +"MDMANDANT= :MDMANDANT,"
                    +"MDSA= :MDSA,"
                    +"MDAUFKRS= :MDAUFKRS,"
                    +"MDAUFLFD= :MDAUFLFD,"
                    +"MDAUFPOS= :MDAUFPOS,"
                    +"MDTYP= :MDTYP,"
                    +"MDAFOLGE= :MDAFOLGE,"
                    +"MDMODNR= :MDMODNR,"
                    +"MDEINSTNR= :MDEINSTNR,"
                    +"MDRPOS= :MDRPOS,"
                    +"MDMAT= :MDMAT,"
                    +"MDVAR= :MDVAR,"
                    +"MDDAT= :MDDAT,"
                    +"MDLO= :MDLO,"
                    +"MDLB= :MDLB,"
                    +"MDLP= :MDLP,"
                    +"MDAUFBKZ= :MDAUFBKZ,"
                    +"MDMENGE= :MDMENGE,"
                    +"MDEINGET= :MDEINGET,"
                    +"MDZUGET= :MDZUGET,"
                    +"MDRESERV= :MDRESERV,"
                    +"MDVERBR= :MDVERBR,"
                    +"MDMEH= :MDMEH,"
                    +"MDMATTYP= :MDMATTYP,"
                    +"MDERLKZ= :MDERLKZ,"
                    +"MDDISPOKZ= :MDDISPOKZ,"
                    +"MDABRKZ= :MDABRKZ,"
                    +"MDFILLER= :MDFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("MDMANDANT", OracleType.NVarChar).Value = dwData["MDMANDANT"].ToString(); 
          or1.Parameters.Add("MDSA", OracleType.NVarChar).Value = dwData["MDSA"].ToString(); 
          or1.Parameters.Add("MDAUFKRS", OracleType.NVarChar).Value = dwData["MDAUFKRS"].ToString(); 
          or1.Parameters.Add("MDAUFLFD", OracleType.NVarChar).Value = dwData["MDAUFLFD"].ToString(); 
          or1.Parameters.Add("MDAUFPOS", OracleType.NVarChar).Value = dwData["MDAUFPOS"].ToString(); 
          or1.Parameters.Add("MDTYP", OracleType.NVarChar).Value = dwData["MDTYP"].ToString(); 
          or1.Parameters.Add("MDAFOLGE", OracleType.NVarChar).Value = dwData["MDAFOLGE"].ToString(); 
          or1.Parameters.Add("MDMODNR", OracleType.NVarChar).Value = dwData["MDMODNR"].ToString(); 
          or1.Parameters.Add("MDEINSTNR", OracleType.NVarChar).Value = dwData["MDEINSTNR"].ToString(); 
          or1.Parameters.Add("MDRPOS", OracleType.NVarChar).Value = dwData["MDRPOS"].ToString(); 
          or1.Parameters.Add("MDMAT", OracleType.NVarChar).Value = dwData["MDMAT"].ToString(); 
          or1.Parameters.Add("MDVAR", OracleType.NVarChar).Value = dwData["MDVAR"].ToString(); 
          or1.Parameters.Add("MDDAT", OracleType.NVarChar).Value = dwData["MDDAT"].ToString(); 
          or1.Parameters.Add("MDLO", OracleType.NVarChar).Value = dwData["MDLO"].ToString(); 
          or1.Parameters.Add("MDLB", OracleType.NVarChar).Value = dwData["MDLB"].ToString(); 
          or1.Parameters.Add("MDLP", OracleType.NVarChar).Value = dwData["MDLP"].ToString(); 
          or1.Parameters.Add("MDAUFBKZ", OracleType.NVarChar).Value = dwData["MDAUFBKZ"].ToString(); 
          or1.Parameters.Add("MDMENGE", OracleType.NVarChar).Value = dwData["MDMENGE"].ToString(); 
          or1.Parameters.Add("MDEINGET", OracleType.NVarChar).Value = dwData["MDEINGET"].ToString(); 
          or1.Parameters.Add("MDZUGET", OracleType.NVarChar).Value = dwData["MDZUGET"].ToString(); 
          or1.Parameters.Add("MDRESERV", OracleType.NVarChar).Value = dwData["MDRESERV"].ToString(); 
          or1.Parameters.Add("MDVERBR", OracleType.NVarChar).Value = dwData["MDVERBR"].ToString(); 
          or1.Parameters.Add("MDMEH", OracleType.NVarChar).Value = dwData["MDMEH"].ToString(); 
          or1.Parameters.Add("MDMATTYP", OracleType.NVarChar).Value = dwData["MDMATTYP"].ToString(); 
          or1.Parameters.Add("MDERLKZ", OracleType.NVarChar).Value = dwData["MDERLKZ"].ToString(); 
          or1.Parameters.Add("MDDISPOKZ", OracleType.NVarChar).Value = dwData["MDDISPOKZ"].ToString(); 
          or1.Parameters.Add("MDABRKZ", OracleType.NVarChar).Value = dwData["MDABRKZ"].ToString(); 
          or1.Parameters.Add("MDFILLER", OracleType.NVarChar).Value = dwData["MDFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
